<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        /*
            函数的默认参
        */

        function add(a, b) {
            a = a === undefined ? 1 : a;
            b = b === undefined ? 1 : b;
            // if(a !== 0){
            //     a = a || 1;
            // }
            // b = b || 1;
            return a + b;
        }
        console.log(add()); // 2
        console.log(add(5)); // 6
        console.log(add(5,9)); // 14

        console.log(6 + null); // 6
        console.log(6 + undefined); // NaN
        console.log(6 + "23"); // '623'
        console.log(6 + true); // 7

		// 引用类型值赋默认值
        function sayName(obj) {
            // if (obj === undefined) {
            //     obj = {name: 'window'}
            // }
            // obj = obj ? obj : {name: 'window'};
            // obj = obj || {name: 'window'}
            // 五星级需要记住的*****
            obj = Object.assign({name: 'window'}, obj)

            console.log(obj.name);
        }

        sayName() // window
        sayName({}) // window
        sayName({name: 'libna'}) // libna

		// es6 默认值  直接在形参后面跟上 = 赋值默认参
        function sum(a = 1, b = 2) {
            console.log(a + b);
        }
        sum(); // 3
        sum(2) // 4
        sum(2,3) // 5

        function sayName1(obj = {name: 'libai'}) {
            console.log(obj.name);
        }
        sayName1(); // 'libai'
        sayName1({}); // undefined
        sayName1({name: 'ss'}); // ss
    </script>
</body>
</html>